package com.taxi.driver.util;

import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;

import com.socks.library.KLog;

import java.util.List;

import static android.content.Context.KEYGUARD_SERVICE;
import static android.content.Context.POWER_SERVICE;

/**
 * Created by Administrator on 2017/9/19.
 */
public class ScreenUtil {
    /**
     * 判断是否锁屏
     *
     * @return 锁屏状态
     */
    public static boolean isLockScreenOn(Context context) {
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        return keyguardManager.inKeyguardRestrictedInputMode();
    }

    /**
     * 唤醒屏幕
     */
    public static void acquireScreen(Context context) {
        PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
        wakeLock.acquire();
    }

    /**
     * 判断是否在后台
     *
     * @return
     */
    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    KLog.i("处于前台：" + appProcess.processName);
                    return false;
                } else {
                    KLog.i("处于后台：" + appProcess.processName);
                    return true;
                }
            }
        }
        return true;
    }

    /**
     * 判断服务是否存在
     *
     * @param name 服务名
     * @return
     */
    public static boolean isServiceWorked(Context context, String name) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceInfos = activityManager.getRunningServices(30);
        for (ActivityManager.RunningServiceInfo serviceInfo : serviceInfos) {
            if (serviceInfo.service.getClassName().equals(name)) {
                KLog.e("服务存在");
                return true;
            }
        }
        KLog.e("服务不存在");
        return false;
    }
}
